// 用户模块
import { loginApi, getUserProfilApi } from '@/api/user'
import { setToken, getToken, removeToken } from '@/utils/auth'

export default {
  namespaced: true,
  state: {
    token: getToken(),
    userInfo: {}
  },
  getters: {
  },
  mutations: {
    // 设置token
    setToken(state, data) {
      state.token = data
      setToken(data)
    },
    // 设置用户资料
    setUserInfo(state, data) {
      console.log(data, 'nnnnn212121');
      state.userInfo = data

    },
    // 清除token
    removeToken(state) {
      state.token = null
      removeToken()
    },
  },
  actions: {
    // 登录操作
    async loginAction(context, data) {
      let res = await loginApi(data)
      // console.log(res);
      context.commit("setToken", res.token)
    },
    // 获取用户资料请求
    async getUserProfileAction({ commit }) {
      let res = await getUserProfilApi()
      console.log(res.name, '用户信息');
      commit('setUserInfo', res)
      return res
    },
    // 退出功能
    quitOut({ commit }) {
      // 清除token
      commit('removeToken')
      // 清除用户资料
      commit('setUserInfo', {})
    }
  }
}